local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "ddzenum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]	
	if seat.rid ~= request.rid then
		return
	end
	if timerid ~= seat.ready_timer_id then
		return
	end
	seat.ready_timer_id = -1
	seat.ready_to_time = 0
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		-----判断朋友桌局数到没有
		if seat ~= nil then
			if table_data.cur_games_times < table_data.conf.game_times then
				-- seat.state = ESeatState.SEAT_STATE_WAIT_START
				-- server.logic.tablelogic.gameready(table_data, request, seat)
				return
			end
			server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	else
		if seat.rid > 0 then
			server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	end
end

function TableTimer.doaction(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	local seat = table_data.seats[request.seat_index]
	if seat then
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
			if seat.is_tuoguan == EBOOL.FALSE then
				seat.timeout_count = seat.timeout_count + 1
				if seat.timeout_count >= table_data.conf.action_timeout_count then
					seat.timeout_count = 0
					server.logic.seatlogic.dealtuoguan(table_data,seat)
				end
			end
		else
			return
		end
	else
		return
	end
	
	table_data.timer_id = -1
	local status, cards
	if request.action_type == EActionType.ACTION_TYPE_MINGPAI or request.action_type == EActionType.ACTION_TYPE_JIAODIZHU then
		if table_data.action_type == EActionType.ACTION_TYPE_MINGPAI then
			-- table_data.state = ETableState.TABLE_STATE_PLAYER_JDZ
			-- server.logic.gamelogic.run(table_data.gamelogic)
			return
		elseif table_data.action_type == EActionType.ACTION_TYPE_JIAODIZHU then
			table_data.action_type = EActionType.ACTION_TYPE_BUJIAO_DIZHU
		elseif table_data.action_type == EActionType.ACTION_TYPE_QIANGDIZHU then
			table_data.action_type = EActionType.ACTION_TYPE_BUQIANGDIZHU
		end
	else
		if table_data.action_type == EActionType.ACTION_TYPE_JIAODIZHU then
			table_data.action_type = EActionType.ACTION_TYPE_BUJIAO_DIZHU
		elseif table_data.action_type ==  EActionType.ACTION_TYPE_QIANGDIZHU then
			table_data.action_type = EActionType.ACTION_TYPE_BUQIANGDIZHU
		elseif table_data.action_type == EActionType.ACTION_TYPE_CHUPAI then
			status, cards = server.logic.tablelogic.putcards(table_data)
			table_data.action_type = EActionType.ACTION_TYPE_CHUPAI
		elseif table_data.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
			if seat.is_tuoguan == EBOOL.TRUE then
				status, cards = server.logic.tablelogic.followcards(table_data, false, true)
				if status == true then
					table_data.action_type = EActionType.ACTION_TYPE_FOLLOW_CHUPAI
				else
					table_data.action_type = EActionType.ACTION_TYPE_CHECK
				end
			else
				table_data.action_type = EActionType.ACTION_TYPE_CHECK
			end
		end
	end

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_SYSTEM then
		local action = timetool.get_time() .. "_" 
			.. seat.rid .. "_"
			.. (table_data.action_type or '0')
		server.logic.tablelogic.save_actions(table_data, action, cards)
	end

	table_data.state = ETableState.TABLE_STATE_CONTINUE
	server.logic.gamelogic.run(table_data.gamelogic)
end

function TableTimer.delete_table(timerid, request)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
		table_data.delete_table_timer_id = -1
        server.delete()
    end
end

function TableTimer.waitstartcountdown(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	server.logic.gamelogic.RiffleandPostCards(table_data)
end


-- 等待明牌定时器时间到,设置牌桌状态为TABLE_STATE_WAIT_PLAYER_JDZ 叫地主阶段
function TableTimer.waitmingpai(timerid,request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	if table_data.state == ETableState.TABLE_STATE_WAIT_CLIENT_ACTION and table_data.action_type == EActionType.ACTION_TYPE_MINGPAI then
		table_data.state = ETableState.TABLE_STATE_PLAYER_JDZ
		table_data.action_type = ETableState.ACTION_TYPE_JIAODIZHU
		server.logic.gamelogic.run(table_data.gamelogic)
	end
end

function TableTimer.dodouble(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	for _, seat in ipairs(table_data.seats) do
		if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
			if not seat.doubled or seat.doubled <= 0 then
				seat.doubled = 1
				msghelper:sendmsg_to_alltableplayer("dodoubleN", {
					rid = seat.rid,
					seat_index = seat.index,
					doubled = 1,
				})
			end
		end
	end
	table_data.doublenum = nil
	if table_data.state ~= ETableState.TABLE_STATE_WAIT_CLIENT_ACTION then
		--------------- 开始出牌 ---------------
		table_data.action_type = EActionType.ACTION_TYPE_CHUPAI
		table_data.action_to_time = timetool.get_time() + table_data.conf.action_timeout
		--下发当前玩家操作协议
		local todontcmsg = {
			rid = table_data.seats[table_data.action_seat_index].rid,
			seat_index = table_data.action_seat_index,
			action_to_time = table_data.action_to_time,
			action_type = table_data.action_type
		}
		----通知所有玩家 seatIndex上的玩家在出牌了
		msghelper:sendmsg_to_alltableplayer("todoN", todontcmsg)
		
		local timerid
		if table_data.seats[table_data.action_seat_index].is_tuoguan == EBOOL.TRUE then
			timerid = timer.settimer(table_data.conf.tuoguan_action_time*100, "doaction", todontcmsg)
		else
			timerid = timer.settimer(table_data.conf.action_timeout*100, "doaction", todontcmsg)
		end
		table_data.timer_id = timerid
		table_data.state = ETableState.TABLE_STATE_WAIT_CLIENT_ACTION
		server.logic.gamelogic.run(table_data.gamelogic)
	end
end

----用于客户端展示牌局游戏结果界面定时器时间到
function TableTimer.onegameend(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	-- 切换房间状态到开启下一局游戏的状态
	table_data.state = ETableState.TABLE_STATE_ONE_GAME_END_AFTER
	server.logic.gamelogic.run(table_data.gamelogic)
end

function TableTimer.onegameendafter(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	--------切换房间状态到开启下一局游戏的状态
	table_data.state = ETableState.TABLE_STATE_ONE_GAME_REAL_END
	server.logic.gamelogic.run(table_data.gamelogic)
end

function TableTimer.disbandtable(timerid)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.vote_timerid ~= timerid then
		return
	end
	-- local noteresultntcmsg = {
	-- 	result = 0,
	-- 	names = table_data.vote_names,
	-- }
	local noteresultntcmsg = {
		table_index = table_data.conf.idx,
		is_success = false,
	}
	if not table_data.vote_num or table_data.vote_num == 0 then
		noteresultntcmsg.is_success = true
	end
	msghelper:sendmsg_to_alltableplayer("disbandresultN", noteresultntcmsg)
	table_data.vote_timerid = nil
	table_data.vote_rid = nil
	table_data.vote_name = nil
	table_data.vote_timeout = nil
	table_data.vote_num = nil
	table_data.vote_answers = nil
	-- table_data.vote_names = nil

	if noteresultntcmsg.is_success then
		if table_data.timer_id and table_data.timer_id > 0 then
			timer.cleartimer(table_data.timer_id)
			table_data.timer_id = -1
		end
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
		-- server.delete(table_data)
		table_data.delete_table_timer_id = timer.settimer(0, "delete_table")
	end
end

return TableTimer